<?php

namespace app\index\controller;

use app\client\index\C_article;

class Article
{
    public $client;

    public function __construct()
    {
        $this->client = new C_article();
    }

    /**
     * 文章列表
     */
    public function index(){
        //获取模块数据
        $moduleId=request()->param("moduleId");
        $moduleData=$moduleId?module($moduleId):array();

        initSeo("index",$moduleId);

        //获取模板配置
        $template_index=setting("template_index",$moduleId);
        $template=$template_index?$template_index:"index/article/index";

        //模板路径
        $template_path=setting("template_path",$moduleId);
        $template_path=$template_path?$template_path:"";

        $data=array();
        $data["module"]=$moduleData;
        $data["request"]=request()->param();

        if($template_path){
            return view($template,$data)->templatePath($template_path);
        }else{
            return view($template,$data);
        }
    }

    /**
     * 文章列表
     */
    public function lists(){
        $moduleId=request()->param("moduleId");
        $page=request()->param("page");
        $moduleData=$moduleId?module($moduleId):array();

        $catId=request()->param("catId");
        $categoryData=($moduleId && $catId)?category('module',$moduleId,$catId):array();
        if($categoryData){
            $categoryData["linkUrl"]=$moduleData['linkUrl'].$categoryData['linkurl'];
        }

        initSeo("list",$moduleId,$catId,$page);

        //获取模板配置
        $template_list=setting("template_list",$moduleId);
        $template="index/article/list";
        if(isset($categoryData["template_list"]) && $categoryData["template_list"]){
            $template=$categoryData["template_list"];
        }else if($template_list){
            $template=$template_list;
        }

        //模板路径
        $template_path=setting("template_path",$moduleId);
        $template_path=$template_path?$template_path:"";

        $data=array();
        $data["module"]=$moduleData;
        $data["category"]=$categoryData;
        $data["request"]=request()->param();

        if($template_path){
            return view($template,$data)->templatePath($template_path);
        }else{
            return view($template,$data);
        }
    }

    /**
     * 文章详情
     */
    public function detail(){
        $moduleId=request()->param("moduleId");
        $mix_articleId=request()->param("mix_articleId");
        if($mix_articleId){
            $articleId=app("yifangConfusionCode")->deCode($mix_articleId);
            request()->setRoute(array("articleId"=>$articleId));
        }
        $articleId=request()->param("articleId");
        $paramsObj = paramsObj();
        $paramsObj->params = array(
            "id"=>$articleId,
        );
        $response = $this->client->info($paramsObj);
        $articleData = array();
        if ($response["success"]) {
            $articleData = $response["data"];
        }
        initSeo("detail",$moduleId,0,0,$articleData);

        $moduleData=$moduleId?module($moduleId):array();
        $categoryData=($moduleId && isset($articleData["categoryId"]) && $articleData["categoryId"])?category('module',$moduleId,$articleData["categoryId"]):array();
        $categoryData["linkUrl"]=$moduleData['linkUrl'].$categoryData['linkurl'];

        //获取模板配置
        $template_content=setting("template_content",$moduleId);
        $template="index/article/detail";
        if(isset($categoryData["template_content"]) && $categoryData["template_content"]){
            $template=$categoryData["template_content"];
        }else if($template_content){
            $template=$template_content;
        }

        $article_404="";
        $article_detail_404=siteSetting("article_detail_404");
        $module_article_detail_404=setting("article_detail_404",$moduleId);
        if($module_article_detail_404){
            $article_404=$module_article_detail_404;
        }else if($article_detail_404){
            $article_404=$article_detail_404;
        }
        $data=array();
        $data["module"]=$moduleData;
        $data["article"]=$articleData;
        $data["category"]=$categoryData;
        $data["request"]=request()->param();
        if(empty($articleData) && $article_404){
            return view($article_404,$data,404);
        }
        if($articleData){
            $paramsObj=paramsObj();
            $paramsObj->params=array(
                "id"=>$articleId,
                "moduleId"=>$moduleId,
                "hits"=>"1",
            );
            $this->client->uploadClickLogs($paramsObj);
        }

        //模板路径
        $template_path=setting("template_path",$moduleId);
        $template_path=$template_path?$template_path:"";
        if($template_path){
            return view($template,$data)->templatePath($template_path);
        }else{
            return view($template,$data);
        }
    }

    /**
     * 上传点击日志
     */
    public function uploadClickLogs(){
        $data=array();
        $data["request"]=request()->param();
        return json($data); //输出测试接口数据
    }
}